/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root1
 * @param {TreeNode} root2
 * @return {TreeNode}
 */
var mergeTrees = function (root1, root2) {
  if (!root1 && !root2) {
    return null;
  } else if (!root1 && root2) {
    root1 = new TreeNode(root2.val, null, null);
  } else if (root1 && !root2) {
    // root2 是空
    return root1;
  } else if (root1 && root2) {
    root1.val += root2.val;
  }
  root1.left = mergeTrees(root1.left, root2.left);
  root1.right = mergeTrees(root1.right, root2.right);
  return root1;
};

const mergeTrees = (t1, t2) => {
  if (t1 == null && t2) {
    return t2;
  }
  if ((t1 && t2 == null) || (t1 == null && t2 == null)) {
    return t1;
  }
  t1.val += t2.val;

  t1.left = mergeTrees(t1.left, t2.left);
  t1.right = mergeTrees(t1.right, t2.right);

  return t1;
};
